package com.framework.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Spring IOC容器工具类 用于提供加载对象、国际化等接口
 */
public class BeanHolder implements ApplicationContextAware {
	/**
	 * Spring 应用上下文
	 */
	private static ApplicationContext context = null;

	/**
	 * 私有构造函数，单态
	 */
	private BeanHolder() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.context.ApplicationContextAware#setApplicationContext
	 * (org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		setApplicationContextStatic(arg0);
	}

	private static void setApplicationContextStatic(ApplicationContext arg0) {
		context = arg0;
	}

	@SuppressWarnings("unchecked")
	public static <E> E getBean(String name) {
		if (null == context) {
			return null;
		} else {
			return (E) context.getBean(name);
		}
	}
}